package com.manage.controller;

import com.beans.CellInfo;
import com.beans.page.PageList;
import com.manage.util.UserUtils;
import com.services.CellInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author jiangwei
 * @since 18/1/13
 */
@Controller
@RequestMapping("cellInfo")
public class CellInfoController extends BaseController {

    @Autowired
    private CellInfoService cellInfoService;

    /**
     * 查询列表
     *
     * @param cellInfo
     * @return
     */
    @RequiresPermissions("cellInfo:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list(CellInfo cellInfo, Long nbhId, HttpServletRequest request) {
        cellInfo.setNbhId(nbhId);
        request.setAttribute("cellInfo", cellInfo);
        return "cellInfo/list";//
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @param cellInfo
     * @return
     */
    @RequiresPermissions("cellInfo:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList, CellInfo cellInfo) {
        cellInfo.setUnitNum(0);
        return cellInfoService.queryPage(pageList, cellInfo);
    }

    /**
     * 新增修改
     *
     * @param cellInfo
     * @return
     */
    @RequiresPermissions("cellInfo:edit")
    @RequestMapping(value = {"add", "edit"}, method = RequestMethod.GET)
    public String edit(CellInfo cellInfo, Long nbhId, Model model) {
        cellInfo.setNbhId(nbhId);
        if(cellInfo.getCellId() != null){
            cellInfo = cellInfoService.queryById(cellInfo.getCellId());
        }
        model.addAttribute("cellInfo", cellInfo);

        return "cellInfo/edit";
    }

    @RequiresPermissions("cellInfo:edit")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(CellInfo cellInfo, HttpServletRequest request){
        int result = -1;
        cellInfo.setCreateBy(UserUtils.getUser().getUserId());
        cellInfo.setUpdateBy(UserUtils.getUser().getUserId());
        if (cellInfo.getCellId() == null) {
            result = cellInfoService.add(cellInfo);
        } else {
            result = cellInfoService.update(cellInfo);
        }
        return "redirect:list?nbhId="+cellInfo.getNbhId();
    }

    @RequiresPermissions("cellInfo:delete")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long cellId) {
        CellInfo cellInfo = new CellInfo();
        cellInfo.setCellId(cellId);
        cellInfo.setUpdateBy(UserUtils.getUser().getUserId());

        int result = cellInfoService.delete(cellInfo);

        //TODO  成功失败结果页面
        return result;
    }
}
